package models

import (
	"database/sql/driver"
	"encoding/json"
	"time"
)

type User struct {
	ID        int       `json:"id" db:"id"`
	Username  string    `json:"username" db:"username"`
	Email     string    `json:"email" db:"email"`
	Password  string    `json:"-" db:"password"`
	Avatar    string    `json:"avatar" db:"avatar"`
	Bio       string    `json:"bio" db:"bio"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

type Topic struct {
	ID               int            `json:"id" db:"id"`
	Title            string         `json:"title" db:"title"`
	Description      string         `json:"description" db:"description"`
	CreatorID        int            `json:"creator_id" db:"creator_id"`
	Creator          User           `json:"creator" db:"creator"`
	Category         string         `json:"category" db:"category"`
	Tags             JSONStringArray `json:"tags" db:"tags"`
	AISummary        string         `json:"ai_summary" db:"ai_summary"`
	CredibilityScore float64        `json:"credibility_score" db:"credibility_score"`
	ViewCount        int            `json:"view_count" db:"view_count"`
	LikeCount        int            `json:"like_count" db:"like_count"`
	CommentCount     int            `json:"comment_count" db:"comment_count"`
	ShareCount       int            `json:"share_count" db:"share_count"`
	Status           string         `json:"status" db:"status"`
	CreatedAt        time.Time      `json:"created_at" db:"created_at"`
	UpdatedAt        time.Time      `json:"updated_at" db:"updated_at"`
}

type Livestream struct {
	ID           int            `json:"id" db:"id"`
	Title        string         `json:"title" db:"title"`
	Description  string         `json:"description" db:"description"`
	TopicID      int            `json:"topic_id" db:"topic_id"`
	Topic        Topic          `json:"topic" db:"topic"`
	StreamerID   int            `json:"streamer_id" db:"streamer_id"`
	Streamer     User           `json:"streamer" db:"streamer"`
	RTMPURL      string         `json:"rtmp_url" db:"rtmp_url"`
	HLSURL       string         `json:"hls_url" db:"hls_url"`
	Status       string         `json:"status" db:"status"`
	ViewerCount  int            `json:"viewer_count" db:"viewer_count"`
	MaxViewers   int            `json:"max_viewers" db:"max_viewers"`
	StartTime    *time.Time     `json:"start_time" db:"start_time"`
	EndTime      *time.Time     `json:"end_time" db:"end_time"`
	Duration     int            `json:"duration" db:"duration"`
	Thumbnail    string         `json:"thumbnail" db:"thumbnail"`
	RecordingURL string         `json:"recording_url" db:"recording_url"`
	IsRecording  bool           `json:"is_recording" db:"is_recording"`
	Tags         JSONStringArray `json:"tags" db:"tags"`
	CreatedAt    time.Time      `json:"created_at" db:"created_at"`
	UpdatedAt    time.Time      `json:"updated_at" db:"updated_at"`
}

type Interaction struct {
	ID         int       `json:"id" db:"id"`
	TopicID    int       `json:"topic_id" db:"topic_id"`
	UserID     int       `json:"user_id" db:"user_id"`
	User       User      `json:"user" db:"user"`
	Type       string    `json:"type" db:"type"`
	Content    string    `json:"content" db:"content"`
	MediaURL   string    `json:"media_url" db:"media_url"`
	MediaType  string    `json:"media_type" db:"media_type"`
	ParentID   *int      `json:"parent_id" db:"parent_id"`
	ReplyCount int       `json:"reply_count" db:"reply_count"`
	LikeCount  int       `json:"like_count" db:"like_count"`
	CreatedAt  time.Time `json:"created_at" db:"created_at"`
	UpdatedAt  time.Time `json:"updated_at" db:"updated_at"`
}

type Media struct {
	ID          int       `json:"id" db:"id"`
	UserID      int       `json:"user_id" db:"user_id"`
	User        User      `json:"user" db:"user"`
	TopicID     *int      `json:"topic_id" db:"topic_id"`
	Topic       *Topic    `json:"topic" db:"topic"`
	Filename    string    `json:"filename" db:"filename"`
	OriginalURL string    `json:"original_url" db:"original_url"`
	Thumbnail   string    `json:"thumbnail" db:"thumbnail"`
	FileType    string    `json:"file_type" db:"file_type"`
	FileSize    int64     `json:"file_size" db:"file_size"`
	Duration    int       `json:"duration" db:"duration"`
	Dimensions  string    `json:"dimensions" db:"dimensions"`
	Status      string    `json:"status" db:"status"`
	CreatedAt   time.Time `json:"created_at" db:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" db:"updated_at"`
}

type Rating struct {
	ID        int       `json:"id" db:"id"`
	UserID    int       `json:"user_id" db:"user_id"`
	User      User      `json:"user" db:"user"`
	TopicID   int       `json:"topic_id" db:"topic_id"`
	Topic     Topic     `json:"topic" db:"topic"`
	Score     int       `json:"score" db:"score"`
	Comment   string    `json:"comment" db:"comment"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
}

type Notification struct {
	ID        int       `json:"id" db:"id"`
	UserID    int       `json:"user_id" db:"user_id"`
	User      User      `json:"user" db:"user"`
	Type      string    `json:"type" db:"type"`
	Title     string    `json:"title" db:"title"`
	Message   string    `json:"message" db:"message"`
	Data      JSONMap   `json:"data" db:"data"`
	IsRead    bool      `json:"is_read" db:"is_read"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
}

// 自定义JSON类型支持
type JSONStringArray []string
type JSONMap map[string]interface{}

func (j JSONStringArray) Value() (driver.Value, error) {
	return json.Marshal(j)
}

func (j *JSONStringArray) Scan(value interface{}) error {
	bytes, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(bytes, j)
}

func (j JSONMap) Value() (driver.Value, error) {
	return json.Marshal(j)
}

func (j *JSONMap) Scan(value interface{}) error {
	bytes, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(bytes, j)
}

// 请求和响应结构体
type RegisterRequest struct {
	Username string `json:"username" binding:"required,min=3,max=20"`
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required,min=6,max=100"`
}

type LoginRequest struct {
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required"`
}

type CreateTopicRequest struct {
	Title       string   `json:"title" binding:"required,min=1,max=100"`
	Description string   `json:"description" binding:"required,min=1,max=500"`
	Category    string   `json:"category" binding:"required"`
	Tags        []string `json:"tags"`
}

type CreateLivestreamRequest struct {
	Title       string   `json:"title" binding:"required,min=1,max=100"`
	Description string   `json:"description" binding:"required,min=1,max=500"`
	TopicID     int      `json:"topic_id" binding:"required"`
	Tags        []string `json:"tags"`
}

type CreateInteractionRequest struct {
	Type      string `json:"type" binding:"required"`
	Content   string `json:"content"`
	MediaURL  string `json:"media_url"`
	MediaType string `json:"media_type"`
	ParentID  *int   `json:"parent_id"`
}

type AISummarizeRequest struct {
	Content   string   `json:"content" binding:"required"`
	Language  string   `json:"language"`
	Keywords  []string `json:"keywords"`
}

type GenerateSolutionRequest struct {
	TopicID   int      `json:"topic_id" binding:"required"`
	Content   string   `json:"content" binding:"required"`
	Type      string   `json:"type" binding:"required"`
	Keywords  []string `json:"keywords"`
}

// JWT Claims
type JWTCustomClaims struct {
	UserID   int    `json:"user_id"`
	Username string `json:"username"`
	Email    string `json:"email"`
	jwt.StandardClaims
}